/*
 * @Author: 13558093597 277098906@qq.com
 * @Date: 2025-06-13 16:37:28
 * @LastEditors: 13558093597 277098906@qq.com
 * @LastEditTime: 2025-08-19 15:48:26
 * @FilePath: \mes_app\lib\route\index.dart
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:mes_app/views/home/controller.dart' show HomeController;
import 'package:mes_app/views/home/index.dart' deferred as home_index;
import 'package:mes_app/views/mine/controller.dart';
import 'package:mes_app/views/mine/info.dart' deferred as mine_info;

class Routes {
  Routes._();

  static const String home = '/';

  static const String mineInfo = '/mine/info';

  static final List<GetPage> getPages = [
    GetPage(
      title: '首页',
      name: home,
      page: () {
        return AsyncRouterPage(home_index.loadLibrary(), (_) {
          return home_index.HomePage();
        });
      },
      binding: BindingsBuilder.put(() => HomeController()),
    ),
    GetPage(
      title: '个人信息',
      name: mineInfo,
      page: () {
        return AsyncRouterPage(mine_info.loadLibrary(), (_) {
          return mine_info.MineInfoPage();
        });
      },
      binding: BindingsBuilder.put(() => MineInfoController()),
    ),
  ];
}

class AsyncRouterPage extends StatelessWidget {
  final Future libraryFuture;

  final WidgetBuilder child;

  const AsyncRouterPage(this.libraryFuture, this.child, {super.key});

  @override
  Widget build(BuildContext context) {
    return FutureBuilder(
      future: libraryFuture,
      builder: (c, s) {
        if (s.connectionState == ConnectionState.done) {
          if (s.hasError) {
            return Scaffold(
              appBar: AppBar(),
              body: Container(
                alignment: Alignment.center,
                child: Text(
                  'Error: ${s.error}',
                  style: const TextStyle(color: Colors.red),
                ),
              ),
            );
          }
          return child.call(context);
        }
        return Scaffold(
          appBar: AppBar(),
          body: Container(
            alignment: Alignment.center,
            child: const CircularProgressIndicator(),
          ),
        );
      },
    );
  }
}
